<?php


namespace App\Modules\Admin\Services\Seller;


use App\Modules\Admin\Entities\Seller\SellerMenuModel;
use App\Modules\Admin\Entities\Seller\SellerRoleModel;
use App\Modules\Admin\Entities\Seller\SellerRoleWithMenu;
use App\Modules\Admin\Services\BaseService;
use Illuminate\Support\Facades\DB;

class SellerRoleServices extends BaseService
{

    public function __construct(SellerRoleModel $seller)
    {
        $this->model = $seller;
        $this->modelRM = new SellerRoleWithMenu();
    }

    public function lists(array $params) : array
    {
        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            /*if (!empty($search = $request->input('search', ''))){
                $search = trim($search);
                $query->where(function($query)use ($search){
                    $query->where('seller_name', 'LIKE', '%' . $search . '%');
                });
            }
            // 置顶
            $set_top = $request->input('set_top', -1);
            if ($set_top > -1){
                $query->where('set_top', '=', $set_top);
            }
            // 推荐
            $is_recommend = $request->input('is_recommend', -1);
            if ($is_recommend > -1){
                $query->where('is_recommend', '=', $is_recommend);
            }
            // 是否公开
            $is_public = $request->input('is_public', -1);
            if ($is_public > -1){
                $query->where('is_public', '=', $is_public);
            }*/
        };
        return parent::lists($params); // TODO: Change the autogenerated stub
    }

    public function getSelectLists($request){
        $first = ['seller_role_id','parent_id','role_name','is_type'];
        $add = ['is_child'=>true];
        $list = $this->model->where(function($query) use ($request){
            $seller_id = $request->input('seller_id',0);
            // 可以按照名称进行搜索
            $query->where('seller_id',$seller_id);
        })->with(['menus'])->get($first)->toArray();
        //数组处理
        array_walk($list,function(&$value){
            $value['tree_menu'] = list_to_tree($value['menus'],'menu_id','parent_id');
            $value['is_child']  = true;
//            $value = array_merge($value, $add);
        }, $add);

        return list_to_tree($list,'seller_role_id','parent_id'); ;
    }
    public function create(array $params)
    {
        DB::beginTransaction();
        try{
            parent::create($params); // TODO: Change the autogenerated stub
            $this->setMenuForRole($params);
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            return false;
        }
    }

    public function update(array $params)
    {
        DB::beginTransaction();
        try{
//            $params['menu_ids'] = implode(',',$params['menu_ids']);
            parent::update($params); // TODO: Change the autogenerated stu
            $this->setMenuForRole($params);
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            return false;
        }
    }

    private function setMenuForRole($params){

        // 超级管理员角色不可编辑
        if (isset($params['menu_ids'])){
            $menu_rules = $params['menu_ids'];

            $all_menus = SellerMenuModel::select('menu_id')->find($menu_rules)->toArray();//当前选中的菜单权限
            $new_all_menus = array_column($all_menus, 'menu_id', 'menu_id');
            $has_menus = $this->detail->menus->toArray();//当前已有的菜单权限
            $new_has_menus = array_column($has_menus, 'menu_id', 'menu_id');

            $primary_key = $this->model->getKeyName();
            /**
             * 添加的权限
             */
            $add_menus = get_array_diff($new_all_menus, $new_has_menus);
//            exit(json_encode($add_menus));
            if (!empty($add_menus)) {
                foreach ($add_menus as $menus) $this->model->grantMenus([
                    $primary_key => $this->detail->{$primary_key},
                    'menu_id' => $menus,
                ]);
            }

            /**
             * 要删除的权限
             */
            $delete_menus = get_array_diff($new_has_menus, $new_all_menus);
            if (!empty($delete_menus)) {
                foreach ($delete_menus as $menus) $this->model->deleteMenus([
                    $primary_key => $this->detail->{$primary_key},
                    'menu_id' => $menus,
                ]);
            }
        }
    }

}